package com.sfx.concurrent.demo.线程基础.线程的创建;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

import static com.sfx.concurrent.demo.线程基础.线程的创建.CreateDemo.getCurThreadName;

@Slf4j(topic = "c.CreateDemo3")
public class CreateDemo3 {
    public static final int MAX_TURN = 5;
    public static final int COMPUTE_TIMES = 100000000;

    //①创建一个 Callable 接口的实现类
    static class ReturnableTask implements Callable<Long> {
        //②编写好异步执行的具体逻辑，可以有返回值
        public Long call() throws Exception {
            long startTime = System.currentTimeMillis();
            log.info(getCurThreadName() + " 线程运行开始.");
            Thread.sleep(1000);
            for (int i = 0; i < COMPUTE_TIMES; i++) {
                int j = i * 10000;
            }
            long used = System.currentTimeMillis() - startTime;
            log.info(getCurThreadName() + " 线程运行结束.");
            return used;
        }
    }

    public static void main1(String args[]) throws InterruptedException {
        ReturnableTask task = new ReturnableTask();//③

        FutureTask<Long> futureTask = new FutureTask<>(task);//④
        Thread thread = new Thread(futureTask, "returnableThread");//⑤
        thread.start();//⑥
        Thread.sleep(500);
        log.info(getCurThreadName() + " 让子弹飞一会儿.");
        log.info(getCurThreadName() + " 做一点自己的事情.");
        for (int i = 0; i < COMPUTE_TIMES / 2; i++) {
            int j = i * 10000;
        }
        log.info(getCurThreadName() + " 获取并发任务的执行结果.");

        try {
            log.info(thread.getName() + "线程占用时间："
                    + futureTask.get());//⑦
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
        log.info(getCurThreadName() + " 运行结束.");
    }
}